App.controller("userInfoCtrl",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	
	$scope.form = {};			//表单
	$scope.form.userInfoEntity = {};   	 	//用户信息
	$scope.shopUserInfo = {};	//商户信息
	$scope.editCode = true;  //组织编号的显示
	$scope.departmentCode = false; //部门编号
	var alertModel = {};	//alert模态框
	
	
	
	//提示信息
	$scope.alertMessage = function(message){
		alertModel.pannel = jqueryAlert({
			content: message,
			modal: true
		});
	}
	
	
	//显示区域名称
	$scope.showArea = function(areaIds){
		$http({
			   url: path+'/server/getregion/'+areaIds,
			   method: 'GET'
		}).success(function(data){ 
			var areaStr = data.result;
			if(areaStr){
				$scope.region_str = areaStr.replace(/,/g,"-");
			}
		}).error(function(data){
			alert(data);   
		})
	}
	
	//获取当前用户信息
	$scope.getCurrentUserInfo = function(id){
		$http({
			   url: path+'/server/getuser/'+id,
			   method: 'GET'
		}).success(function(data){  
			if(data.code==0){  //成功
				$scope.shopUserInfo = data.result;
				$scope.id = $scope.shopUserInfo.id;
				$scope.form.userInfoEntity.id = $scope.shopUserInfo.userInfoEntity.id;
				$scope.form.userInfoEntity.principal = $scope.shopUserInfo.userInfoEntity.principal;	//负责人
				$scope.form.userInfoEntity.phone_num = $scope.shopUserInfo.userInfoEntity.phone_num;	//联系方式
//				$scope.password = $scope.shopUserInfo.pwd;	//后台密码
				$scope.password = "******";
				publicService.showUploadImage("logo_img","logo_url",$scope.shopUserInfo.userInfoEntity.imgurl);
				$scope.showArea($scope.shopUserInfo.userInfoEntity.region_code);
				publicService.showUploadImage("upload1_img","upload1_url",$scope.shopUserInfo.commerceEntity.biz_url);
				
			}else if(data.code==1){  //失败
				
			}
		}).error(function(data){
			alert(data);   
		})
	}
	//监听密码更改
	$scope.myFunc = function(){
		$scope.password = "";
	}
	//密码修改失去焦点
	$scope.myFunc2 = function(){
		if($scope.password.length>=6){
			$scope.form.id = $scope.id;
			$scope.form.pwd = $scope.password;
		}else{
			alert("后台密码 的长度最少是6位");
			 $scope.password = "";
		}

	}
	//表单提交
	$scope.formSubmit = function(){
		if(!$scope.form.userInfoEntity.principal){
			$scope.alertMessage("请输入【负责人】！");
			return;
		}
		if(!$scope.form.userInfoEntity.phone_num){
			$scope.alertMessage("请输入【联系方式】！");
			return;
		}
		if(!$scope.password){
			$scope.alertMessage("请输入【后台密码】！");
			return;
		}		
		$http({
			   url: path+'/server/modifyuser',
			   method: 'POST',
			   data: angular.toJson($scope.form),  
		}).success(function(data){
			if(data.code==0){
				if($scope.form.pwd){
					alert("密码已修改成功，请重新登录！");
					
					location.href=path;
				}else{
					window.location.reload();
				}
				
			}else{
				$scope.alertMessage("失败！");
			}
		}).error(function(data){
			alert(data);   
		})
	}
	
	
	
	
	
	
	$scope.getCurrentUserInfo(userInfo.id);		//获取当前用户信息
	

	if(userInfo.user_level==0){	//开发者
		$scope.highestShow = true;
	}else if(userInfo.user_level==3){	//三级用户
		$scope.highestShow = true;
		$scope.editCode = false;  //组织编号的显示
	}else if(userInfo.user_level==2){	//二级用户
		$scope.highestShow = false;
	}else if(userInfo.user_level==1){	//一级用户
		$scope.highestShow = false;
		$scope.departmentCode = true; //部门编号
	}
	
	
	
	

	
});